<?php

namespace app\api\service;

use app\api\lib\SuPengJun;
use app\common\model\circle\CircleFansModel;
use app\common\model\circle\CircleModel;
use app\common\model\dynamic\DynamicLikeModel;
use app\common\model\dynamic\DynamicModel;
use app\common\model\goods\GoodsProductModel;
use app\common\model\order\OrderGoodsModel;
use app\common\model\order\OrderModel;
use app\common\model\user\UserActivityModel;
use app\common\model\user\UserCardModel;
use app\common\model\user\UserCartModel;
use app\common\model\user\UserFollowModel;
use app\common\model\user\UserPrivacyModel;

class UserService
{
    public
    static function user_find()
    {
        return ['id,name,intro,avatar,gender,age,mobile,country,province,city'];
    }

    public
    static function user_other($info, $uid = 0, $userId = 0)
    {
        $info['refresh_time'] = strtotime('tomorrow 00:00:00');
        $info['mobile'] = $info['mobile'] ? SuPengJun::mobileStr($info['mobile']) : '';
        $province = $info['province'];
        $city = $info['city'];
        if (!$province) $province = $info['city'];
        if (!$province) $province = $info['country'];
        if (!$city) $province = $info['province'];
        if (!$city) $province = $info['country'];
        $info['province'] = SuPengJun::addsStr($province);
        $info['city'] = SuPengJun::addsStr($city, 1);
        $condition[] = ['user_id', '=', $info['id']];
        $info['fans_count'] = UserFollowModel::where('follow_id', $info['id'])->count();
        $info['follow_count'] = UserFollowModel::where($condition)->count();
        $ids = DynamicModel::where($condition)->column('id');
        $likeCountMax = DynamicModel::where($condition)->sum('likes');
        $info['like_count'] = DynamicLikeModel::whereIn('dynamic_id', $ids)->count() + $likeCountMax;
        $info['like_count_str'] = SuPengJun::convert($info['like_count']);
        $info['circle_count'] = CircleFansModel::where($condition)->count();
        if ($uid == 0 && $userId == 0) {
            $info['privacy'] = UserPrivacyModel::where($condition)->field('like,search,follow')->find();
            $circleId = CircleFansModel::where($condition)->order('id', 'desc')->value('circle_id');
            $info['circle_img'] = $circleId ? CircleModel::where('id', $circleId)->value('avatar') : '';
            $info['cart_count'] = UserCartModel::where($condition)->count();
            $goodsProductId = UserCartModel::where($condition)->order('id', 'desc')->value('goods_product_id');
            $info['cart_img'] = $goodsProductId ? GoodsProductModel::where('id', $goodsProductId)->value('img') : '';
            $info['order_count'] = OrderModel::where($condition)->where('type', 0)->where('status', '<>', 6)->count();
            $orderId = OrderModel::where($condition)->where('type', 0)->where('status', '<>', 6)->order('id', 'desc')->value('id');
            $info['order_img'] = $orderId ? OrderGoodsModel::where('order_id', $orderId)->value('product_img') : '';
            $info['activity_count'] = UserActivityModel::where($condition)->count();
            $info['activity_img'] = UserActivityModel::where($condition)->value('activity_img');
            $info['card_count'] = UserCardModel::where($condition)->where('status', 1)->count();
        } else {
            $info['is_follow'] = UserFollowModel::where('user_id', $uid)->where('follow_id', $info['id'])->value('id') > 0;
            if ($info['circle_count'] > 0) {
                $cid = CircleFansModel::where($condition)->column('circle_id');
                $info['circle'] = CircleModel::whereIn('id', $cid)->field('id,name,avatar')->select();
            } else {
                $info['circle'] = [];
            }
            $info['privacy'] = UserPrivacyModel::where($condition)->field('like,follow')->find();
        }
        return $info;
    }
}
